package net.entelijan.cobean.examples.showcase.config.combobox;

import junit.framework.Assert;
import net.entelijan.cobean.data.common.Country;
import net.entelijan.cobean.data.common.Language;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JComboBoxFixture;
import org.junit.Test;

public class ComboboxFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(ComboboxFestTestCase.class);

	@Test
	public void testInit() {
		ConfigBookAuthorComboPanel panel = new ConfigBookAuthorComboPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookAuthorComboShowcase sc = new ConfigBookAuthorComboShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JComboBoxFixture languageComboBox = frameFix.comboBox("authorLanguageComboBox");
			JComboBoxFixture countryComboBox = frameFix.comboBox("authorCountryComboBox");
			
			sc.init(panel);
			pause(100);
			
			languageComboBox.requireSelection("ENGLISH");
			countryComboBox.requireSelection("INDIA");
			
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testChangeFromGUI() {
		ConfigBookAuthorComboPanel panel = new ConfigBookAuthorComboPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookAuthorComboShowcase sc = new ConfigBookAuthorComboShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JComboBoxFixture languageComboBox = frameFix.comboBox("authorLanguageComboBox");
			JComboBoxFixture countryComboBox = frameFix.comboBox("authorCountryComboBox");
			
			sc.init(panel);
			pause(100);
			
			languageComboBox.selectItem(2);
			countryComboBox.selectItem(1);
			pause(100);
			
			Assert.assertEquals("ITALIAN", sc.getBook().getAuthor().getLanguage().toString());
			Assert.assertEquals("ISRAEL", sc.getBook().getAuthor().getCountry().toString());
			
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testChangeFromModel() {
		ConfigBookAuthorComboPanel panel = new ConfigBookAuthorComboPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookAuthorComboShowcase sc = new ConfigBookAuthorComboShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JComboBoxFixture languageComboBox = frameFix.comboBox("authorLanguageComboBox");
			JComboBoxFixture countryComboBox = frameFix.comboBox("authorCountryComboBox");
			
			sc.init(panel);
			pause(100);
			
			sc.getBook().getAuthor().setLanguage(Language.GERMAN);
			sc.getBook().getAuthor().setCountry(Country.ISRAEL);
			pause(100);
			
			languageComboBox.requireSelection("GERMAN");
			countryComboBox.requireSelection("ISRAEL");
			
			
		} finally {
			frameFix.cleanUp();
		}

	}


	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
